home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 January: Mac OS SDK / Dev.CD Jan 98 SDK1.toast / Development Kits (Disc 1) / Apple Shared Library Manager / ASLM Examples / Example Tools / Sources / TMacSemaphoreExample.cp < prev    next >
Encoding:
Text File  |  1996-11-19  |  3.1 KB  |  101 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        TMacSemaphoreExample.cp
  3.  
  4.     Contains:    This module shows an example use of TMacSemaphore.
  5.  
  6.     Copyright:    © 1993-1994 by Apple Computer, Inc., all rights reserved.
  7.  
  8. */
  9.  
  10. #include "TInitSLM.h"            // the TInitSLM class and SLM include files
  11.  
  12. #include <Events.h>
  13. #include <LowMem.h>
  14.  
  15. ///————————————————————————————————————————————————————————————————————————————————————
  16. ///    PROTOTYPES
  17. ///————————————————————————————————————————————————————————————————————————————————————
  18.  
  19. static void move_cursor( Point newMouse);
  20.  
  21. /*————————————————————————————————————————————————————————————————————————————————————
  22.     main 
  23.     
  24.     since the Macintosh OS supports only one thread of execution, the only way that 
  25.     data can be changed while you are trying to access it is for the data to be changed
  26.     by an interrupt. One way to demonstrate is by changing the cursor position, since
  27.     the cursor can change at interrupt time. Not only do we have to disable interrupts
  28.     when we change the cursor position, but we also have to check to see if the cursor
  29.     is already busy. If it is then we have to re-enable interrupts so then cursor
  30.     can become "unbusy".
  31. ————————————————————————————————————————————————————————————————————————————————————*/
  32.  
  33. main() 
  34. {
  35.     TInitSLM initLibraryManager;            // initialize the shared library manager
  36.     
  37.     if( initLibraryManager.Failed() )        // If we failed, let's go home
  38.         return 1;
  39.     
  40.     TMacSemaphore        *semaphore = new TMacSemaphore;
  41.  
  42.     TStopwatch        stopwatch;
  43.     while( stopwatch.ElapsedSeconds() < 2) {    // do this for 2 seconds
  44.  
  45.         // we need to go into a loop until we can successfully grab the semaphore
  46.         // while the cursor isn't busy.
  47.         while( true ) {
  48.             semaphore->Grab();                // disable interrupts, so we can change cursor
  49.             
  50.             if( ! LMGetCrsrBusy() )            // if cursor isn't busy our grab is good
  51.                 break;
  52.         
  53.             semaphore->Release();            // release and try again
  54.         }
  55.  
  56.         Point        newmouse;            
  57.         
  58.         SetPt( &newmouse, 0,0 );            // move to cursor to 0,0
  59.         move_cursor( newmouse );
  60.         
  61.         semaphore->Release();                // release
  62.     }
  63.     
  64.     delete semaphore;                        // deallocate the space used
  65.     
  66.     return 0;
  67. }
  68.  
  69. /*————————————————————————————————————————————————————————————————————————————————————
  70.     move_cursor
  71.     
  72.     move the cursor to the new position passed. Please, don't try this at home. This
  73.     is only to demonstrate the semaphores.
  74. ————————————————————————————————————————————————————————————————————————————————————*/
  75.  
  76. //
  77. //    Some enums stolen from the old SysEqu.h file.
  78. //
  79. enum {
  80.  MTemp = 0x828,                    /*[GLOBAL VAR]  Low-level interrupt mouse location [long]*/
  81.  RawMouse = 0x82C,                /*[GLOBAL VAR]  un-jerked mouse coordinates [long]*/
  82.  CrsrNew = 0x8CE,                /*[GLOBAL VAR]  Cursor changed? [byte]*/
  83.  CrsrState = 0x8D0                /*[GLOBAL VAR]  Cursor nesting level [word]*/
  84. };
  85.  
  86. static void move_cursor( Point newMouse )
  87. {
  88.     long         *vh;
  89.     
  90.     vh = (long *)&newMouse;
  91.     
  92.     HideCursor();                            // Hide  the cursor since we are changing it
  93.  
  94.     *((long *)RawMouse) = *vh;                // force the mouse position to top of scren
  95.     *((long *)MTemp) = *vh;                    // also the temporary position
  96.     *((short *)CrsrState) = 0;                // indicate the cursor has changed
  97.     *((char *)CrsrNew) = 1;                    // indicate a new cursor
  98.  
  99.     ShowCursor();                            // redisplay the cursor now
  100. }
  101.